package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.tbit.uqbike.constant.RedisConstant;
import com.tbit.uqbike.object.pojo.Machine;
import com.tbit.uqbike.object.pojo.TerAlarm;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.base.MachineService;
import com.tbit.uqbike.service.base.RedisService;
import com.tbit.uqbike.service.business.TerAlarmService;
import com.tbit.uqbike.webmanager.dao.history.TerAlarmDao;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("terAlarmService")
public class TerAlarmServiceImpl implements TerAlarmService {
    @Autowired
    private TerAlarmDao terAlarmDao;

    @Autowired
    private RedisService redisService;
    @Autowired
    private MachineService machineService;

    @Override
    public List<TerAlarm> getByMachineNOAndTime(String machineNO, Integer alarmType, Long startTime, Long endTime, Integer rowCount, Integer pageNO) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("machineNO", machineNO);
        params.put("alarmType", alarmType);
        params.put("startTime", new Date(startTime));
        params.put("endTime", new Date(endTime));
        params.put("start", (pageNO - 1) * rowCount);
        params.put("rowCount", rowCount);
        List<TerAlarm> history = terAlarmDao.getByMachineNOAndTime(params);

        return history;
    }

    @Override
    public Integer getTotal(String machineNO, Integer alarmType, Long startTime, Long endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("machineNO", machineNO);
        params.put("alarmType", alarmType);
        params.put("startTime", new Date(startTime));
        params.put("endTime", new Date(endTime));

        return terAlarmDao.getTotal(params);
    }

    @Override
    public void recAlarm(TerAlarm terAlarm) {
        /**单独处理类型为8的告警*/
        if (terAlarm.getAlarmType().equals(8)) {
            Machine machine = machineService.getByMachineNO(terAlarm.getMachineNO());
            if (machine == null) {
                return;
            }
            Integer num = 1;
            String tmp = redisService.get(RedisConstant.ALARM_KEY_PREFIX + machine.getAccountId());
            if (tmp != null) {
                num = num + Integer.valueOf(tmp);
            }
            String format = DateUtil.format(new Date(), "yyyyMMddHH");
            redisService.add(RedisConstant.ALARM_KEY_PREFIX + machine.getAccountId() + "#" + format, String.valueOf(num), 7 * 24 * 3600);
        }
//        /**查询设备信息*/
//        if (SystemData.machineNOMachineMap.size() == 0) {
//            /**同步数据*/
//            //quartzJobService.synMachineData();
//        }
//
//        MachineSimple machine = SystemData.machineNOMachineMap.get(terAlarm.getMachineNO());
//        /**过滤掉震动告警*/
//        if (machine != null && terAlarm.getAlarmType() != 1) {
//            String msgId = UUID.randomUUID().toString();
//            AlarmMsg alarmMsg = new AlarmMsg();
//            alarmMsg.setAccountId(machine.getAccountId());
//            alarmMsg.setMachineId(machine.getMachineId());
//            alarmMsg.setMsgId(msgId);
//            alarmMsg.setMachineNO(terAlarm.getMachineNO());
//            alarmMsg.setAlarmType(terAlarm.getAlarmType());
//            alarmMsg.setDt(DateTimeUtil.getDateTimeByGL(Long.parseLong(terAlarm.getDt())));
//            alarmMsg.setAlarmExMsg(terAlarm.getAlarmExMsg() == null ? "" : terAlarm.getAlarmExMsg());
//            alarmMsg.setAlarmExCode(terAlarm.getAlarmExCode() == null ? "" : StringUtil.getListString(terAlarm.getAlarmExCode()));
//
//            /**添加到缓存列表*/
//            SystemData.alarmMsgs.add(alarmMsg);
//        }
    }


    /**
     * 导出
     * @param machineNO 设备编号
     * @param alarmType 类型
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 文件名
     */
    @Override
    public Result<Object> exportExcel(String machineNO, Integer alarmType, Long startTime, Long endTime, String userCode) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String fileName = String.format("%s~%s 历史告警信息", sdf.format(new Date(startTime)), sdf.format(new Date(endTime)));
        List<TerAlarm> terAlarmList = getByMachineNOAndTime(machineNO, alarmType, startTime, endTime, 65535, 1);
        if (CollUtil.isNotEmpty(terAlarmList)) {
            for (TerAlarm ta : terAlarmList) {
                ta.setUserCode(userCode);
            }
        }
        ExcelUtil<TerAlarm> excelUtil = new ExcelUtil<>(TerAlarm.class);
        JsonResult result = excelUtil.exportExcel(terAlarmList, fileName);
        return Result.success(result.getData());
    }

}  